package cn.dac.shiro.realm;

import cn.dac.entity.security.SysPermission;
import cn.dac.entity.security.SysRole;
import cn.dac.entity.security.SysUser;
import cn.dac.service.PermissionService;
import cn.dac.service.RoleService;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UserRealm extends AbstractUserRealm {
    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;




    @Override
    public UserRolesAndPermissions doGetGroupAuthorizationInfo(SysUser sysUser) {
        Set<String> userRoles = new HashSet<>();
        Set<String> userPermissions = new HashSet<>();
        //TODO 获取用户组的权限信息
        return new UserRolesAndPermissions(userRoles, userPermissions);
    }

    @Override
    public UserRolesAndPermissions doGetRoleAuthorizationInfo(SysUser sysUser) {
        Set<String> userRoles = new HashSet<>();
        Set<String> userPermissions = new HashSet<>();
        //获取当前用户下拥有的所有角色列表
        List<SysRole> roles = roleService.findByUserId(sysUser.getId());
        for (SysRole role : roles) {
            userRoles.add(role.getRole());
            List<SysPermission> permissions = permissionService.findByRoleId(role.getId());
            for (SysPermission permission : permissions){
                userPermissions.add(permission.getUrl());
            }
        }
        System.out.println("#######获取角色："+userRoles);
        System.out.println("#######获取权限："+userPermissions);
        return new UserRolesAndPermissions(userRoles, userPermissions);
    }



}
